package com.example.demo;

import com.example.demo.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import javax.jws.soap.SOAPBinding;
import java.util.HashMap;
import java.util.Map;


@SpringBootTest
@RunWith(SpringRunner.class)
public class GetForEntityAndGetForObject {
    @Autowired
    RestTemplateBuilder restTemplateBuilder;
    @Test
    public void nparameter(){
        RestTemplate client =restTemplateBuilder.build();
        ResponseEntity<String> responseEntity=client.getForEntity("http://localhost:8080/getuser1",String.class);
        System.out.println(responseEntity.getBody());
    }
    @Test
    public void withparameters1(){
        RestTemplate client =restTemplateBuilder.build();
        ResponseEntity<String> responseEntity=client.getForEntity("http://localhost:8080/getparameter?name={1}&id={2}",String.class,"lixingzhen",3);
        System.out.println(responseEntity.getBody());
    }
    @Test
    public void withparameters2(){
        RestTemplate client=restTemplateBuilder.build();
        Map<String,String> map=new HashMap<>();
        map.put("name","lixingzhen2");
        ResponseEntity<String> responseEntity=client.getForEntity("http://localhost:8080/getparameter?name={name}&id=3",String.class,map);
        System.out.println(responseEntity.getBody());
    }
    @Test
    public void restUser1(){
        RestTemplate client=restTemplateBuilder.build();
        ResponseEntity<User> responseEntity=client.getForEntity("http://localhost:8080/getuser1",User.class);
        System.out.println(responseEntity.getBody().getId());
        System.out.println(responseEntity.getBody().getName());
    }


    /**
     *
     * getForObject
     */
    @Test
    public void getForObject(){
        RestTemplate client=restTemplateBuilder.build();
        User user=client.getForObject("http://localhost:8080/getuser1", User.class);//得到一个user对象
        System.out.println(user.getName());
    }
}
